import { customRef } from 'vue'

// customRef 自定义了一个ref的功能
export function useDebouncedRef (value, delay = 200) {
  let timeout
  return customRef((track, trigger) => {
    // track get执行函数
    return {
      get () {
        track()
        return value
      },
      set (newValue) {
        clearTimeout(timeout)
        timeout = setTimeout(() => {
          value = newValue
          // trigger set执行函数
          trigger()
        }, delay)
      }
    }
  })
}